<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.example.demo.grbkxt.model.Blog" %>
<%@ page import="com.example.demo.grbkxt.model.User" %>
<!DOCTYPE html>
<html>
<head>
    <title>用户仪表板</title>
    <%@ include file="/WEB-INF/views/common/header.jsp" %>
    <style>
        .blog-item {
            border: 1px solid #ddd;
            margin: 10px 0;
            padding: 15px;
            border-radius: 5px;
        }
        .blog-item h3 {
            margin-top: 0;
        }
        .blog-item a {
            color: #333;
            text-decoration: none;
        }
        .blog-item a:hover {
            color: #007bff;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>欢迎来到用户仪表板</h1>
            <div class="user-info">
                <span>欢迎, <%= ((User)session.getAttribute("user")).getUsername() %></span>
                <a href="${pageContext.request.contextPath}/logout" class="btn btn-danger">退出</a>
            </div>
        </div>
        
        <div class="filter-bar">
            <form action="${pageContext.request.contextPath}/blog/search" method="get" class="search-form">
                <select name="category">
                    <option value="">所有分类</option>
                    <option value="技术" ${selectedCategory == '技术' ? 'selected' : ''}>技术</option>
                    <option value="生活" ${selectedCategory == '生活' ? 'selected' : ''}>生活</option>
                    <option value="随笔" ${selectedCategory == '随笔' ? 'selected' : ''}>随笔</option>
                    <option value="其他" ${selectedCategory == '其他' ? 'selected' : ''}>其他</option>
                </select>
                <input type="text" name="keyword" value="${keyword}" placeholder="搜索博客...">
                <button type="submit">搜索</button>
            </form>
        </div>

        <div class="blog-list">
            <% 
            List<Blog> blogs = (List<Blog>)request.getAttribute("blogs");
            if(blogs != null && !blogs.isEmpty()) {
                for(Blog blog : blogs) { 
            %>
                <div class="blog-item">
                    <h3>
                        <a href="${pageContext.request.contextPath}/blog/view?id=<%= blog.getId() %>">
                            <%= blog.getTitle() %>
                        </a>
                    </h3>
                    <div class="blog-meta">
                        <span>分类：<%= blog.getCategory() %></span>
                        <span>发布时间：<%= blog.getCreateTime() %></span>
                    </div>
                </div>
            <% 
                }
            } else { 
            %>
                <p>暂无博客文章</p>
            <% } %>
        </div>
    </div>
    <script>
        function filterByCategory(category) {
            document.getElementById('currentCategory').value = category;
            document.querySelector('.search-form').submit();
        }
        </script>
</body>
</html>
